package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.ActivityProps;
import com.sc.nft.entity.DigitalCollectionProductInfo;
import com.sc.nft.entity.DigitalCollectionProductInfoAuction;
import com.sc.nft.entity.DigitalCollectionProductInfoSubscribe;
import com.sc.nft.entity.dto.AuctionRecordAdminDTO;
import com.sc.nft.entity.vo.AuctionRecordAdminPageVO;
import com.sc.nft.entity.vo.UserAuctionRecordPageVO;
import com.sc.nft.mapper.DigitalCollectionProductInfoAuctionMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @Author：LiGen
 * @Package：com.sc.nft.dao
 * @Project：sc_metago
 * @name：DigitalCollectionProductInfoAuctionDao
 * @Date：2024/9/5 14:57
 * @Filename：DigitalCollectionProductInfoAuctionDao
 */
@Slf4j
@Repository
public class DigitalCollectionProductInfoAuctionDao extends ServiceImpl<DigitalCollectionProductInfoAuctionMapper, DigitalCollectionProductInfoAuction> {


    public DigitalCollectionProductInfoAuction getByProductInfoAuction(Long id) {
        LambdaQueryWrapper<DigitalCollectionProductInfoAuction> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DigitalCollectionProductInfoAuction::getProductId, id);
        return baseMapper.selectOne(wrapper);
    }


    /**
     * 根据商品id修改
     *
     * @param byProductInfoSubscribe
     */
    public Boolean updateByProductId(DigitalCollectionProductInfoAuction byProductInfoSubscribe) {
        LambdaUpdateWrapper<DigitalCollectionProductInfoAuction> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DigitalCollectionProductInfoAuction::getProductId, byProductInfoSubscribe.getProductId());
        wrapper.set(DigitalCollectionProductInfoAuction::getAuctionPrice, byProductInfoSubscribe.getAuctionPrice());
        wrapper.set(DigitalCollectionProductInfoAuction::getAuctionNum, byProductInfoSubscribe.getAuctionNum());
        wrapper.set(DigitalCollectionProductInfoAuction::getAuctionStartTime, byProductInfoSubscribe.getAuctionStartTime());
        wrapper.set(DigitalCollectionProductInfoAuction::getAuctionEndTime, byProductInfoSubscribe.getAuctionEndTime());
        wrapper.set(DigitalCollectionProductInfoAuction::getParticipationType, byProductInfoSubscribe.getParticipationType());
        wrapper.set(DigitalCollectionProductInfoAuction::getConsumptionNum, byProductInfoSubscribe.getConsumptionNum());
//        wrapper.set(DigitalCollectionProductInfoAuction::getHaveResult, byProductInfoSubscribe.getHaveResult());
        wrapper.set(DigitalCollectionProductInfoAuction::getRuleImg, byProductInfoSubscribe.getRuleImg());
        return update(wrapper);
    }

    public DigitalCollectionProductInfoAuction getByProductId(Long productId) {
        LambdaQueryWrapper<DigitalCollectionProductInfoAuction> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DigitalCollectionProductInfoAuction::getProductId, productId);
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 获取活动时间已结束并且未生成拍卖结果的列表
     *
     * @param now
     * @return
     */
    public List<DigitalCollectionProductInfoAuction> getNotHaveResultList(Date now) {
        LambdaQueryWrapper<DigitalCollectionProductInfoAuction> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DigitalCollectionProductInfoAuction::getHaveResult, Boolean.FALSE);
        wrapper.lt(DigitalCollectionProductInfoAuction::getAuctionEndTime, now);
        return baseMapper.selectList(wrapper);
    }

    /**
     * 后台拍卖记录
     *
     * @param auctionRecordAdminDTO
     * @return
     */
    public Page<AuctionRecordAdminPageVO> auctionRecordAdminPage(AuctionRecordAdminDTO auctionRecordAdminDTO) {
        return baseMapper.auctionRecordAdminPage(auctionRecordAdminDTO.getFirstTitle(), auctionRecordAdminDTO.getAuctionStatus(), auctionRecordAdminDTO.toMp());
    }

    public Boolean updateHaveResult(Long id, Boolean haveResult) {
        LambdaUpdateWrapper<DigitalCollectionProductInfoAuction> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DigitalCollectionProductInfoAuction::getId, id);
        wrapper.set(DigitalCollectionProductInfoAuction::getHaveResult, haveResult);
        return update(wrapper);
    }

}
